/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("wsz_shebei",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using ATkg.BizAppProject.DomainModels;
using ATkg.BizAppProject.IServices;
using DGZImp.Core.EFDbContext;
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json.Linq;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using DocumentFormat.OpenXml.Wordprocessing;
using DGZImp.Core.DbSqlSugar;

namespace ATkg.BizAppProject.Controllers
{
    public partial class wsz_shebeiController
    {
        private readonly Iwsz_shebeiService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Imaterial_plan_xieRepository _repository;//访问数据库
        public ISqlSugarClient Db = AutofacContainerModule.GetService<ServiceDbContext>()?.SqlSugarClient;

        [ActivatorUtilitiesConstructor]
        public wsz_shebeiController(
            Iwsz_shebeiService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        [HttpGet("shebeitaizhang")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<JsonResult> shebeitaizhang()
        //public async Task<JsonResult> SyncProductionTask_wgjy([FromBody] PageDataOptions order_no_)

        {
            Console.WriteLine("get方法调用");
            var r = await Service.get_info_from_bfmes();
            //var r = 0;

            //var r = 0;
            return JsonNormal(r);
        }

        [HttpGet("shebei_db_taizhang")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<JsonResult> shebei_db_taizhang()
        //public async Task<JsonResult> SyncProductionTask_wgjy([FromBody] PageDataOptions order_no_)

        {
            Console.WriteLine("get方法调用");
            var rows = await Service.get_devices_db_taizhang();
            //var r = 0;

            //var r = 0;
            return JsonNormal(rows);
        }

        [HttpGet("shebei_db_for_dj")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<JsonResult> shebei_db_for_dj()
        //public async Task<JsonResult> SyncProductionTask_wgjy([FromBody] PageDataOptions order_no_)

        {
            Console.WriteLine("get方法调用");
            var rows = await Service.get_dj_from_bfmes();
            //var r = 0;

            //var r = 0;
            return JsonNormal(rows);
        }

        [HttpGet("dianjian_devices")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<string> dianjian_devices(string mes_data)
        //public async Task<JsonResult> SyncProductionTask_wgjy([FromBody] PageDataOptions order_no_)

        {
            Console.WriteLine("get方法调用1111");
            JArray mes_data_final = JArray.Parse(mes_data);
            Console.WriteLine(mes_data_final[0]);
            //Console.WriteLine(shebei_type);
            //Console.WriteLine(mesid_);
            var info_ = mes_data_final[0];
            string shebei_type = (string)info_["shebei_type"];
            int mesid = (int)info_["mesid"];
            string r = await Service.dianjian_wsz_devices(shebei_type, mesid);
            //string r = "k";
            //var r = 0;

            //var r = 0;
            return r;
        }

        [HttpGet("wb_devices")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<string> wb_devices(string mes_data)
        {
            Console.WriteLine("get方法调用1111");
            JArray mes_data_final = JArray.Parse(mes_data);
            Console.WriteLine(mes_data_final[0]);
            var info_ = mes_data_final[0];
            string shebei_type = (string)info_["shebei_type"];
            int mesid = (int)info_["mesid"];
            string wb_name = (string)info_["wb_name"];
            string r = await Service.wb_wsz_devices(shebei_type, mesid, wb_name);
            return r;
        }


        [HttpGet("weibao_devices")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<JsonResult> weibao_devices()
        //public async Task<JsonResult> SyncProductionTask_wgjy([FromBody] PageDataOptions order_no_)

        {
            Console.WriteLine("get方法调用");
            var rows = await Service.get_wb_from_bfmes();
            //var r = 0;

            //var r = 0;
            return JsonNormal(rows);
        }

        [HttpGet("wb_done_final")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<string> wb_done_final(string mes_data)
        {
            Console.WriteLine("get方法调用1111");
            JArray mes_data_final = JArray.Parse(mes_data);
            Console.WriteLine(mes_data_final);
            var info_ = mes_data_final[0];
            string r = await Service.wb_wsz_done_devices((string)info_["option_type"], mes_data_final, (int)info_["id"]["id"], "aa");
            return r;
        }


        [HttpGet("base_data_search")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<JsonResult> base_data_search()
        //public async Task<JsonResult> SyncProductionTask_wgjy([FromBody] PageDataOptions order_no_)

        {
            Console.WriteLine("get方法调用");
            var rows = await Service.get_base_data_in();
            //var r = 0;

            //var r = 0;
            return JsonNormal(rows);
        }


        [HttpGet("base_data_input")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<string> base_data_input(string mes_data)
        {
            Console.WriteLine("get方法调用1111");
            JArray mes_data_final = JArray.Parse(mes_data);
            Console.WriteLine(mes_data_final);
            var info_ = mes_data_final[0];
            Console.WriteLine(info_);
            Console.WriteLine(mes_data_final[1]);

            // 解析JSON为JArray
            //JArray mes_data_final = JArray.Parse(mes_data);

            // 1. 正确获取二维数组（mes_data_final[1]是一个JArray）
            JArray dataDetail = mes_data_final[1] as JArray;
            

            string r = await Service.base_data_input_detail((string)info_, dataDetail);
            //return r;

            return "成功";
        }

        [HttpGet("delete_data_id")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<JsonResult> delete_data_id(int id)
        {
            Console.WriteLine("get方法调用",id);
            var rows = await Service.delete_base_data_in(id);
            return JsonNormal(rows);
        }

        [HttpGet("search_base_data_all")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<JsonResult> search_base_data_all()
        {
            var rows = await Service.search_base_data_all_api();
            return JsonNormal(rows);
        }

        public class wsz_data_in
        {
            [SugarColumn(IsPrimaryKey = true)] // 主键标识（若存在）
            public string device_name { get; set; }
            public string data_name { get; set; }
            public string data_detail { get; set; }
            public string intime { get; set; }
            public string savetime { get; set; }
            public int Id { get; set; }
            //public string data_detail { get; set; }
            //public string mesid { get; set; }
            //public string dj { get; set; }

        }

        [HttpGet("Get_gongx")]
        [AllowAnonymous]
        //[HttpBody]
        public async Task<Object> Get_gongx(string data_info)
        {
            Console.WriteLine(data_info);
            JObject mes_data_final = JObject.Parse(data_info);
            Console.WriteLine(data_info);
            Console.WriteLine(mes_data_final["1"]);
            //string[] date_riqi_list = mes_data_final["2"];
            string[] date_riqi_list = mes_data_final["2"].ToObject<string[]>();
            string[] zhou = mes_data_final["3"].ToObject<string[]>();
            Console.WriteLine(date_riqi_list);
            Console.WriteLine(date_riqi_list.Length);
            string pk_step = (string)mes_data_final["1"];
            var rows = await Service.get_db_get_db_eng(pk_step, date_riqi_list,zhou);
            //Console.WriteLine(rows);
            //JArray row_ = (JArray)rows;

            
            return rows;
        }
    }
}
